// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import HeroContact, {colliderGroup} from "./HeroContact";
import {animName} from "./HeroControl";
import Component = cc.Component;
import Music, {musicName} from "../PublicScript/Music";
import GameControl_red_blue_man from "./GameControl_red_blue_man";

const {ccclass, property} = cc._decorator;

export enum switchColor {
    yellow,
    violet,
    green,
    blue,
    orange,
    red,
}

export enum switchAngle {
    thirty = 30,
    loseThirty = -30,
}

@ccclass
export default class SwitchControl extends cc.Component {

    @property({tooltip: "对应的事件", type: cc.Component.EventHandler})
    toolEvent: cc.Component.EventHandler[] = [];
    @property({tooltip: "对应的联动事件", type: cc.Component.EventHandler})
    btnEvent: cc.Component.EventHandler[] = [];
    @property({tooltip: "是否有其他按钮联动"})
    linkage: boolean = false;
    @property({tooltip: "初始的方向角", type: cc.Enum(switchAngle)})
    switchAngle: switchAngle = switchAngle.thirty;
    @property({tooltip: "是否开启旋转"})
    angle: boolean = false;
    @property({tooltip: "是否开启按压"})
    press: boolean = false;
    @property({tooltip:"需要移动的节点",type: cc.Node})
    moveNode: cc.Node = null;

    // onLoad () {}
    //初始位置
    initPos = cc.v2();
    isPass = false;
    //计算次数
    count = 0;
    //告诉上面我是否已经被 联动锁了， 不做下一步操作
    isLinkage = false;
    //
    isOnce = false;

    start () {
        this.init();
    }

    init(){
        if(this.angle){
            this.node.angle = this.switchAngle;
        }else{
            this.node.getPosition(this.initPos);
        }
    }

    //是否不在连锁下踩在按钮上方
    noLink = false;

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve(contact, self, other) {
        if(other.node.group == colliderGroup.hero && other.node.getComponent(HeroContact).animState == animName.run && this.angle){
            if(this.actionLock){
                return;
            }
            let normal = contact.getWorldManifold().normal;
            if(this.node.angle > 0 && normal.x < 0){
                this.action();
            }else if(this.node.angle < 0 && normal.x > 0){
                this.action();
            }
        }
    }

    isContact = false;
    // 只在两个碰撞体结束接触时被调用一次
    onBeginContact(contact, self, other) {
        if((other.node.group == colliderGroup.hero || other.node.group == colliderGroup.box)  && this.press && other.tag == 0 && !this.isLinkage){
            this.count++;
            if(this.linkage){
                cc.Component.EventHandler.emitEvents(this.btnEvent, -1)
            }
            if(this.count == 1){
                this.passAnim(-1);
            }
        }
    }

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact, self, other) {
        if((other.node.group == colliderGroup.hero || other.node.group == colliderGroup.box)  && this.press && other.tag == 0 && !this.isLinkage){
            this.count--;
            if(this.count <= 0){
                if(this.linkage){
                    cc.Component.EventHandler.emitEvents(this.btnEvent, 1)
                }
                this.passAnim(1);
            }
        }
    }

    actionLock = false;
    dir = 1;
    action(){
        this.actionLock = true;
        Music.GamePlayEffect(musicName.Remote_sensing_sound);
        this.node.runAction(
            cc.sequence(
                cc.rotateTo(0.2, -this.node.angle),
                cc.callFunc(()=>{
                    this.actionLock = false;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, [this.dir]);
                    this.dir = -this.dir;
                })
            )
        )
    }

    linkageEvent(dir){
        this.isLinkage = dir == -1;
        this.node.getComponent(cc.PhysicsPolygonCollider).enabled = !this.isLinkage;
        this.passAnim(dir);
    }

    canPlayEffect = true;
    passAnim(dir){
        if(this.canPlayEffect){
            Music.GamePlayEffect(musicName.button_sound_game);
        }
        this.canPlayEffect = false;
        this.moveNode.stopAllActions();
        let y = dir == -1 ? this.initPos.y - 20 : this.initPos.y;
        this.moveNode.runAction(
            cc.sequence(
                cc.moveTo(0.1, cc.v2(this.initPos.x, y)),
                cc.callFunc(()=>{
                    if(this.initPos.y == this.node.y){
                        this.canPlayEffect = true;
                    }
                    cc.Component.EventHandler.emitEvents(this.toolEvent, [-dir]);
                })
            )
        )
    }

    update (dt) {

    }
}
